package cn.schoolwow.util.domain.query.dictionary;

import cn.schoolwow.quickflow.QuickFlowBuilder;
import cn.schoolwow.util.module.query.dictionary.domain.DictionaryContext;
import cn.schoolwow.util.module.query.dictionary.domain.QueryDictionaryOption;
import cn.schoolwow.util.module.query.dictionary.service.AddDictionaryContextFlow;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class QueryDictionary {
    private QueryDictionaryOption queryDictionaryOption = new QueryDictionaryOption();

    /**
     * 创建查询字典对象
     * */
    public static QueryDictionary newQuery(){
        QueryDictionary queryDictionary2 = new QueryDictionary();
        return queryDictionary2;
    }

    /**指定替换数据对象*/
    public QueryDictionary setDataObject(JSONObject dataObject){
        queryDictionaryOption.dataObject = dataObject;
        return this;
    }

    /**指定替换数据数组对象*/
    public QueryDictionary setDataArray(JSONArray dataArray){
        queryDictionaryOption.dataArray = dataArray;
        return this;
    }

    /**
     * 设置字典项
     * @param fieldName 字典名称
     * @param dictionaryValue 字典值,格式为 key1:value1,key2:value2
     * */
    public QueryDictionary addDictionary(String fieldName, String dictionaryValue){
        QuickFlowBuilder.startFlow(new AddDictionaryContextFlow())
                .putThreadLocalInstanceData(queryDictionaryOption)
                .putCurrentCompositeFlowData("fieldName", fieldName)
                .putCurrentCompositeFlowData("dictionaryValue", dictionaryValue)
                .execute();
        return this;
    }

    /**
     * 添加字典替换字段
     * @param fieldName 字典名称
     * @param dictionaryValue 字典值,格式为 key1:value1,key2:value2
     * */
    public QueryDictionary addReplaceDictionary(String fieldName, String replaceFieldName, String dictionaryValue){
        QuickFlowBuilder.startFlow(new AddDictionaryContextFlow())
                .putThreadLocalInstanceData(queryDictionaryOption)
                .putCurrentCompositeFlowData("fieldName", fieldName)
                .putCurrentCompositeFlowData("replaceFieldName", replaceFieldName)
                .putCurrentCompositeFlowData("dictionaryValue", dictionaryValue)
                .execute();
        return this;
    }

    /**
     * 设置字典项
     * @param dictionaryContext 字典项
     * */
    public QueryDictionary addDictionary(DictionaryContext dictionaryContext){
        queryDictionaryOption.dictionaryContextList.add(dictionaryContext);
        return this;
    }

    public QueryDictionaryResponse execute(){
        return new QueryDictionaryResponse(queryDictionaryOption);
    }

}
